<template>
	<div class="wrapper">

		<!-- header部分 -->
		<header>
			<p>商家注册</p>
			<!-- 返回键 -->
			<GoBackButton></GoBackButton>
		</header>

		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					商家用户名：
				</div>
				<div class="content">
					<input type="text" @blur="checkSellerId" v-model="business.sellerId" placeholder="商家用户名">
				</div>
			</li>
			<li>
				<div class="title">
					商家名称：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessName" placeholder="商家名称">
				</div>
			</li>
			<li>
				<div class="title">
					商家类别：
				</div>
				<div class="content" style="font-size: 3vw;">
					<input type="radio" v-model="business.orderTypeId" value="0" style="width:6vw;height: 3.2vw;">全部
					<input type="radio" v-model="business.orderTypeId" value="1" style="width:6vw;height: 3.2vw;">美食
					<input type="radio" v-model="business.orderTypeId" value="2" style="width:6vw;height: 3.2vw;">早餐
					<input type="radio" v-model="business.orderTypeId" value="3" style="width:6vw;height: 3.2vw;">跑腿代购
					<input type="radio" v-model="business.orderTypeId" value="4" style="width:6vw;height: 3.2vw;">汉堡披萨
					<input type="radio" v-model="business.orderTypeId" value="5" style="width:6vw;height: 3.2vw;">甜品饮品
					<input type="radio" v-model="business.orderTypeId" value="6" style="width:6vw;height: 3.2vw;">速食简餐
					<input type="radio" v-model="business.orderTypeId" value="7" style="width:6vw;height: 3.2vw;">地方小吃
					<input type="radio" v-model="business.orderTypeId" value="8" style="width:6vw;height: 3.2vw;">米粉面馆
					<input type="radio" v-model="business.orderTypeId" value="9" style="width:6vw;height: 3.2vw;">包子粥铺
					<input type="radio" v-model="business.orderTypeId" value="10" style="width:6vw;height: 3.2vw;">炸鸡炸串
				</div>
			</li>
		</ul>

		<div class="button-login">
			<button @click="BusinessRegister">注册商家</button>
		</div>

		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>

<script>
	import Footer from '../components/Footer.vue';
	import GoBackButton from '../components/GoBackButton.vue';

	export default {
		name: 'BusinessRegister',
		data() {
			return {
				user: {},
				business: {
					businessName: '',
					businessImg: '',
					orderTypeId: 0,
					userId: '',
					sellerId: ''
				}
			}
		},
		created() { //获取当前用户信息
			this.user = this.$getSessionStorage('user');
			this.business.userId = this.user.userId;
		},
		methods: {
			
			checkSellerId() {
				console.log(this.business.sellerId);
				this.$axios.get('businesses/sellerId/' + this.business.sellerId
				).then(response => {
					let str = response.data.businessId;
					console.log(str.businessId);
					if(str != '' || str != null || str != 'null' || str != undefined){
						this.business.sellerId = '';
						alert('此商家用户名已存在！')
					}
				}).catch(error => {
					console.error(error);
				});
			},

			//注册商家
			BusinessRegister() {
				if (this.business.businessName == '') {
					alert('商家名称不能为空！');
					return;
				}
				if (this.business.sellerId == '') {
					alert('商家自定义用户名不能为空！');
					return;
				}

				//注册请求
				this.$axios.post(
					'businesses',
					JSON.stringify(
						this.business
					), {
						headers: {
							'Content-Type': "application/json"
						}
					}
				).then(response => {
					if (response.data > 0) {
						alert('注册成功！');
						this.$router.go(-1);
					} else {
						alert('注册失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			}
		},
		components: {
			Footer,
			GoBackButton
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-login {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-login button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #666;
		background-color: #EEE;
		border-radius: 4px;

		border: none;
		outline: none;
		border: solid 1px #DDD;
	}
</style>
